//
//  TuiSong.m
//  PCAForWar
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TuiSong.h"
#import <DKNightVersion.h>

@interface TuiSong () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableViewForPush;
@property (nonatomic, retain) NSArray *arrForPush;
@property (nonatomic, retain) UISwitch *swUp;
@property (nonatomic, retain) UISwitch *swDown;

@end

@implementation TuiSong

- (void)dealloc{
    
    _tableViewForPush.dataSource = nil;
    _tableViewForPush.delegate = nil;
    
    [_tableViewForPush release];
    [_arrForPush release];
    [_swUp release];
    [_swDown release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"推送设置";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"AmericanTypewriter-Bold" size:22]}];
    
    [self handleData];
    [self createtableView];
    [self createSwitchForPush];
}

#pragma mark 夜间模式
- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor redColor], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.tableViewForPush.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
}

- (void)handleData {
    
    self.arrForPush = @[@"要闻推送", @"夜间推送"];
    
}

- (void)createtableView {
    
    self.tableViewForPush = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    self.tableViewForPush.delegate = self;
    self.tableViewForPush.dataSource = self;
    
    self.tableViewForPush.bounces = NO;
    
    [self.view addSubview:self.tableViewForPush];
    
    [_tableViewForPush release];
}

#pragma mark 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            
            return self.arrForPush.count;
            
            break;
            
        default:
            
            return 0;
            
            break;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForPush"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"poolForPush"];
        
    }
    
    // 取消点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.text = self.arrForPush[indexPath.row];
    
    // 夜间模式改变颜色
    cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    return cell;
}

#pragma mark 要闻推送开关
- (void)createSwitchForPush {
    
    self.swUp = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 5 * 4, 100, 0, 0)];
    
    [self.view addSubview:self.swUp];
    
    [self.swUp addTarget:self action:@selector(switchUpAction:) forControlEvents:UIControlEventValueChanged];
    
    [_swUp release];
}

- (void)switchUpAction:(UISwitch *)swUp {
    
    if (swUp.isOn) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已为您开启推送功能" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"确定" style:1 handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:actionDelete];
        
        /** 模态推出 */
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        self.swDown = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 5 * 4, 160, 0, 0)];
        
        [self.view addSubview:self.swDown];
        
        [self.swDown addTarget:self action:@selector(switchDownAction:) forControlEvents:UIControlEventValueChanged];
        
        [_swDown release];
        
    } else {
        
        // 隐藏
        self.swDown.hidden = YES;
        
    }
    
}

- (void)switchDownAction:(UISwitch *)swDown {
    
    if (swDown.isOn) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"开启后,晚22:00 - 早晨6:00将接受推送" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"确定" style:1 handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:actionDelete];
        
        /** 模态推出 */
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
